<?php

/**
 * @name: rayhelper.class.php
 * @version: 1.0
 * @package: PHP MVC Framework
 * @uses: Base class for all helper classes
 * @copyright: MIT
 * @author Andrew Lowther
 */

class RayHelper {
	
	public function js_bundle($dir) {
		
		$html = "";
		
		$prefix = 'js/';
		
		if (is_dir($prefix . $dir)) {
			
			if ($od = opendir($prefix . $dir)) {
				
				while (($files = readdir($od)) !== false) {
					
					$ext = end(explode('.', $files));
					
					if ($ext == 'js') {
						
						$html .= "<script script type=\"text/javascript\" src=\"/" . $prefix . $dir . $files . "\"></script>\n";
						
					}
					
				}
				
				closedir($od);
				
			}
			
		}
		
		return $html;
		
	}
	
	public function css_bundle($dir) {
		
		$html = "";
		
		$prefix = 'css/';
		
		if (is_dir($prefix . $dir)) {
			
			if ($od = opendir($prefix . $dir)) {
				
				while (($files = readdir($od)) !== false) {
					
					$ext = end(explode('.', $files));
					
					if ($ext == 'css') {
						
						$html .= "<link rel=\"stylesheet\" media=\"all\" type=\"text/css\" href=\"/" . $prefix . $dir . $files . "\" />\n";
						
					}
					
				}
				
				closedir($od);
				
			}
			
		}
		
		return $html;
		
	}
	
	public function shorten_url($url, $login, $appkey, $format = 'xml', $version = '2.0.1') {
		
		$bitly = 'http://api.bit.ly/shorten?version=' . $version . '&longUrl=' . urlencode($url) . '&login=' . $login . '&apiKey=' . $appkey . '&format=' . $format;
		
		$response = file_get_contents($bitly);
		
		if (strtolower($format) == 'json') {
			
			$json = @json_decode($response, true);
			return $json['results'][$url]['shortUrl'];
			
		} else {
			
			$xml = simplexml_load_string($response);
			return 'http://bit.ly/' . $xml->results->nodeKeyVal->hash;
			
		}
		
	}
	
}

?>